<?php

class C_Cmslink extends C_Abstract
{

    protected $_id = 0;
    protected $_space = '';
    protected $_idx = '';
    protected $_key = '';
    protected $_cinfo = array();
    protected $_table = 't_cms_link';

    public function __construct()
    {
        parent::__construct();
        $this->_id = getRequest('id', '0');
        $this->_space = getRequest('space', '');
        $this->_idx = getRequest('idx', '');
        $this->_key = getRequest('key', '');
        if (!$this->_space || !$this->_idx) {
            \SF\Controller\Router::redirect('/?a=right');
        }

        $this->smt->assign('id', $this->_id);
        $this->smt->assign('space', $this->_space);
        $this->smt->assign('idx', $this->_idx);
        $this->smt->assign('key', $this->_key);

        // 获取当前分类信息
        $sql = 'SELECT * FROM t_cms_class WHERE space="' . $this->_space . '" AND idx="' . $this->_idx . '"';
        $this->_cinfo = $this->db->fetchRow($sql);
        $settings = json_decode($this->_cinfo['settings'], true);
        $settings['i_subtitle']['name'] || $settings['i_subtitle']['name'] = '副标题';
        $settings['i_link']['name'] || $settings['i_link']['name'] = '链接';
        $settings['i_img']['name'] || $settings['i_img']['name'] = '图片';
        $this->_cinfo['settings'] = $settings;
        $this->smt->assign('cinfo', $this->_cinfo);
    }

    public function indexAction()
    {
        $sql = 'SELECT SQL_CALC_FOUND_ROWS * FROM ' . $this->_table . ' WHERE space="' . $this->_space . '" AND idx="' . $this->_idx . '" ORDER BY sort DESC' . $this->_limit;
        $list = $this->db->fetchAll($sql);

        $this->smt->assign('page_html', $this->getPageHtml());
        $this->smt->assign('list', $list);
        $this->smt->display('cms/link_list.html');
    }

    public function editAction()
    {
        if ($this->_id) {
            $sql = 'SELECT * FROM ' . $this->_table . ' WHERE id="' . $this->_id . '"';
            $info = $this->db->fetchRow($sql);
            $this->smt->assign('info', $info);
        }
        $this->smt->display('cms/link_edit.html');
    }

    public function sortAction()
    {
        if ($this->_id) {
            $t = getGet('t', 'up');
            $sql = 'SELECT sort FROM ' . $this->_table . ' WHERE id=' . $this->_id;
            $cur_sort = $this->db->fetchOne($sql);

            if ($t == 'up') {
                $op = '>=';
                $st = 'asc';
            } else {
                $op = '<=';
                $st = 'desc';
            }
            $sql = 'SELECT id, sort FROM ' . $this->_table . ' WHERE sort' . $op . $cur_sort . ' ORDER BY sort ' . $st . ' LIMIT 2';
            $result = $this->db->fetchAll($sql);
            if (count($result) == 2) {
                $this->db->update($this->_table, array('sort' => $result[1]['sort']), 'id=' . $result[0]['id']);
                $this->db->update($this->_table, array('sort' => $result[0]['sort']), 'id=' . $result[1]['id']);
            }
        }
        \SF\Controller\Router::redirect('/?c=cmslink&space=' . $this->_space . '&idx=' . $this->_idx .'&page='. $this->_page);
    }

    public function delAction()
    {
        // 获取内容
        if ($this->_id) {
            $sql = 'SELECT * FROM ' . $this->_table . ' WHERE id="' . $this->_id . '"';
            $info = $this->db->fetchRow($sql);
            // 删除图片
            if ($info['img']) {
                @unlink(UP_PATH . '/' . $info['img']);
            }
            // 删除缩略图片
            if ($info['thumb']) {
                @unlink(UP_PATH . '/' . $info['thumb']);
            }
            // 最后删除内容
            $this->db->delete($this->_table, array('id=?' => $this->_id));
        }
        \SF\Controller\Router::redirect('/?c=cmslink&space=' . $this->_space . '&idx=' . $this->_idx);
    }

    public function saveAction()
    {
        $domain = Config::get('domain');
        $content = getPost('content', '', 'stripslashes');
        $content = str_replace('./../uploads/attach', $domain . '/uploads/attach', $content);
        $data = array(
            'space' => $this->_space,
            'idx' => $this->_idx,
            'title' => getPost('title', '', 'stringTrim'),
            'subtitle' => getPost('subtitle', '', 'stringTrim'),
            'link' => getPost('link', '', 'stringTrim'),
            'content' => $content,
        );


        if (!empty($_FILES['image']['size'])) {
            require_once INC_PATH . '/upload.cls.php';
            require_once 'PhpThumb/ThumbLib.inc.php';

            $sub_path = 'img/' . date('Ym');
            $up_path = UP_PATH . '/' . $sub_path;
            $up = new Upload($up_path);
            $up_img = $up->upOneFile('image');

            // 图片缩略处理
            $img_deal = getPost('img_deal', '');
            $img_width = getPost('img_width', '');
            $img_height = getPost('img_height', '');
            if ($img_deal && $img_width && $img_height) {
                $img = PhpThumbFactory::create($up_path . '/' . $up_img['new_name']);
                if ($img_deal == 'recut') {
                    $img->adaptiveResize($img_width, $img_height);
                } else {
                    $img->resize($img_width, $img_height);
                }
                $img->save($up_path . '/' . $up_img['new_name']);
            }
            $data['img'] = $sub_path . '/' . $up_img['new_name'];

            // 删除老图片
            $old_img = getPost('old_img', '');
            @unlink(UP_PATH . '/' . $old_img);


            if (getPost('thumb_has', '0')) {
                $thumb_width = getPost('thumb_width', '0', 'int');
                $thumb_height = getPost('thumb_height', '0', 'int');

                $thumb = PhpThumbFactory::create($up_path . '/' . $up_img['new_name']);
                $thumb->resize($thumb_width, $thumb_height);
                $thumb->save($up_path . '/t_' . $up_img['new_name']);
                $data['thumb'] = $sub_path . '/t_' . $up_img['new_name'];

                // 删除老图片
                $old_thumb = getPost('old_thumb', '');
                @unlink(UP_PATH . '/' . $old_thumb);
            }
        }
        $data['status'] = 1;
        $data['sort'] = time();

        $this->db->save('t_cms_link', $data, array('id=?' => $this->_id));
        \SF\Controller\Router::redirect('/?c=cmslink&space=' . $this->_space . '&idx=' . $this->_idx);
    }

}